/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package operators;

import metadata.IndexMetaData;
import metadata.SystemMetaData;
import metadata.TableMetaData;

/**
 *
 * @author Dragan
 */
public class IndexScan extends Operation {

    protected SystemMetaData systemMetaData;
    protected TableMetaData tableMetaData;
    protected IndexMetaData indexMetaData;
    protected long inputSize;
    protected double selectivityofPredicate;

    public IndexScan(SystemMetaData systemMetaData, TableMetaData tableMetaData, IndexMetaData indexMetaData, long inputSize, double selectivityofPredicate) {
        this.systemMetaData = systemMetaData;
        this.tableMetaData = tableMetaData;
        this.indexMetaData = indexMetaData;
        this.inputSize = inputSize;
        this.operationName = "IndexScan";
        this.selectivityofPredicate = selectivityofPredicate;
        execute();
    }

    @Override
    protected void execute() {
        double h = indexMetaData.getHeightOfBPlusTree();
        double nl = indexMetaData.getNumberOfLeaves();
        double nk = indexMetaData.getDistinctKeyValues();
        double nb = indexMetaData.getDiskPagesToStoreABitmap();
        double nt = tableMetaData.getNumberOfTuples();
        double sel = selectivityofPredicate;
        this.diskCost = (long) ((h + nl * sel + nk * sel) * nb);
        this.outputSize = (long) (nt * sel);
    }

    @Override
    public String toString() {
        String str = " t:" + tableMetaData.getTableName() + " input:" + this.inputSize;
        return this.operationName + str + " " + super.toString();
    }
}
